<?php
	$sport = new Sport($_GET['sport']);

	$url_file = "https://www.interwetten.com/xmlfeeder/feeder.asmx/getfeed?FEEDPARAMS=BettingOffers|LANGUAGE=EN|OFFERTYPEIDS=13,20,32,33,37,39,88,9905353,9905554,9924883,9926105,9930764|KINDOFSPORTIDS=".$sport->getBookID();

	parseOdds($url_file, $sport->getID());
	
	// LES FONCTIONS TRAITANT LE FLUX XML
	
	function parseOdds($url_file, $sportID){
		$fp = fopen($url_file, 'r', false); 
		$response = stream_get_contents($fp); 
		fclose($fp); 
		$response = simplexml_load_string($response); 
		$leagues = $response->children()->children();  
		
		leaguesTraitement($leagues, $sportID);
	}
	
	function leaguesTraitement($leagues, $sportID){
		echo "leaguesTraitement on sport ".$sportID."<br>";
		foreach($leagues as $league)
		{
			$leagueID =	$league->attributes()->ID;
			$leagueName =	$league->attributes()->NAME;
			
			$newLeague = new League($leagueID);
			if(!$newLeague->getExist()){				
				$newLeague->setSport($sportID);
				$newLeague->setName("EN", $leagueName);
			
				$newLeague->create();
			}
			
			$events = $league->children();
			eventsTraitement($events, $leagueID);
		}
	}
	
	function eventsTraitement($events, $leagueID){
		echo "------ eventsTraitement on league ".$leagueID."<br>";
		
		foreach($events as $event)
		{
			$eventId = $event->attributes()->EVENTID;
			$eventName = $event->attributes()->NAME;		
			
			$eventStartDateHour = explode("T", $event->attributes()->START_TIME);
			$eventStartDate = $eventStartDateHour[0];
			$eventStartHour = $eventStartDateHour[1];
			
			// enregistrement de la rencontre
			$newEvent = new Event($eventId);
			
			$newEvent->setDate($eventStartDate);
			$newEvent->setHour($eventStartHour);
			
			if(!$newEvent->getExist()){	// Creation
				$newEvent->setLeague($leagueID);
				$newEvent->create();
			}else{						// Update
				$newEvent->save();
			}
			
			// récuperation des bets
			$bets =$event->children();	
	
			betsTraitement($bets, $eventId);
	
		}
	}
	
	function betsTraitement($bets, $eventId){
		echo "------------ betsTraitement on event ".$eventId."<br>";
		$teamsUpdated = false;
		$allowedTypes = getAllowedType();
		
		foreach($bets as $bet)
		{
			$betId = $bet->attributes()->ID;
			$betTypeId = $bet->attributes()->TYPEID;			
			$betTeam1 = $bet->attributes()->PLAYER1;
			$betTeam2 = $bet->attributes()->PLAYER2;
			$betTip = $bet->attributes()->TIP;
			$betQuote = floatval(str_replace(",", ".", $bet->attributes()->QUOTE));
			
			// le bet fait il partie des types autorisé ?
			if(in_array($betTypeId , $allowedTypes)){				
				echo "Autorisé <br/>";
				$newBet = new Bet($betId);
				
				// rearrangement des informations
				$betTip = getRealBetTip($betTypeId, $betTip, $betTeam2);
				$betOption = getBetOption($betTypeId, $betTeam2);
				
				echo "Pari : " . $betTip . " " .$betOption."<br>";
				
				// mise a jour du bet
				$newBet->setEvent($eventId);
				$newBet->setType($betTypeId);
				$newBet->setTip($betTip);
				$newBet->setOption($betOption);
				$newBet->setQuote($betQuote);
					
				var_dump($newBet);
				
				if(!$newBet->getExist()){										
					$newBet->create();
				}else{
					$newBet->save();
				}
				
				// mise a jour des equipes de l'evenement
				if($betTypeId == 13 && !$teamsUpdated){ // 13 = resultat du match
					echo "Update Team <br/>";
					$event = new Event($eventId);
					$event->setTeam("home", $betTeam1);
					$event->setTeam("away", $betTeam2);
					$event->save();
					$teamsUpdated = true;
				}
			}			
		}
	}

	function getAllowedType(){
		global $wpdb;
		
		$return = array();
		
		$_rows = $wpdb->get_results($wpdb->prepare("SELECT id FROM bettypes"));
		foreach($_rows as $_row){
			$return[sizeof($return)] = $_row->id;
		}
		
		return $return;
	}
	
	function getRealBetTip($betTypeId, $betTip, $betTeam2){
		$return = $betTip;
		
		switch ($betTypeId){			
			case 20 :		// double chance : tip est dans team2
			case 33 :		// score exact : tip est dans team2
			case 9926105 :	// les 2 équipes marque : tip est dans team2
				$return = $betTeam2;
				break;
			case 9924883 : 	// underover 1.5 : on remplace par less ou more
				$initial = array("0-1", "2 or more");
				$final   = array("UNDER", "OVER");				
				$return = str_replace($initial, $final, $betTeam2);
				break;
			case 37 : 		// underover 2.5 : on remplace par less ou more
				$initial = array("0-2", "3 or more");
				$final   = array("UNDER", "OVER");			
				$return = str_replace($initial, $final, $betTeam2);
				break;
			case 9930764 : 	// underover 3.5 : on remplace par less ou more
				$initial = array("0-3", "4 or more");
				$final   = array("UNDER", "OVER");				
				$return = str_replace($initial, $final, $betTeam2);
				break;
			case 88 :		// underover_usa : tip = premier partie de team2
				$temp = explode(" ", $betTeam2);
				$return = strtoupper($temp[0]);
				break;
		}
		
		return $return;
	}
	
	function getBetOption($betTypeId, $betTeam2){
		$return = "";
		
		switch ($betTypeId){
			case 32 : 		// handicap_0_1 : handicap away = 1
				$return = 1;
				break;
			case 9905353 :	// handicap_1_0 : handicap away = -1
				$return = -1;
				break;
			case 9924883 :	// underover 1.5
				$return = 1.5;
				break;
			case 37 :		// underover 2.5
				$return = 2.5;
				break;
			case 9930764 :	// underover 3.5
				$return = 3.5;
				break;
			case 39 :		// handicap_usa : extraction du handicap equipe away
			case 9905554 : 	// hadicap_rugby : extraction du handicap equipe away
				$temp = strrchr($betTeam2, '(');
				$initial = array("(", ")");
				$final   = array("", "");				
				$temp = str_replace($initial, $final, $temp);
				$return = $temp; 
				break;
			case 88 :		// underover_usa : option = deuxieme partie de team2
				$temp = explode(" ", $betTeam2);
				$return = $temp[1];
				break;
		}
		
		return $return;
	}
?>